package com.xxl.sodmall.mvp.presenter

import android.text.TextUtils
import com.dgshanger.sanhxiaofeisc.common.AppApi
import com.dgshanger.sanhxiaofeisc.common.Constants
import com.dgshanger.sanhxiaofeisc.entity.LoginEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.LoginContract
import com.dgshanger.sanhxiaofeisc.mvp.model.LoginModel
import com.dgshanger.sanhxiaofeisc.ui.login.LoginActivity
import com.xx.baseuilibrary.mvp.BaseMvpPresenter
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observers.DefaultObserver
import java.util.concurrent.TimeUnit

/**
 * LoginPresenter
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/2/5 17:19.
 */

class LoginPresenter : BaseMvpPresenter<LoginContract.Model, LoginContract.View>(), LoginContract.Presenter {

    override fun sendCode() {

        val phone = getView()?.phone
        if (TextUtils.isEmpty(phone) || phone?.length != 11) {
            getView()?.showToast("请输入手机号")
            return
        }

        getModel().sendCode(phone, object : XxBaseHttpObserver<Any>() {

            override fun onCompleted(msg: String?, entity: Any?) {
                getView()?.showToast(msg)
                //开始倒计时
                val timeCount = 60
                Observable.interval(0, 1, TimeUnit.SECONDS)
                        .take((timeCount + 1).toLong())
                        .map { aLong -> timeCount - aLong!! }
                        .doOnSubscribe {
                            //发送数据中,将发送按钮状态设置为不可用
                            getView()?.setSendBtnEnable(false)
                        }
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(object : DefaultObserver<Long>() {
                            override fun onNext(value: Long?) {
                                getView()?.setSendBtnText("(" + value + "s)")
                            }

                            override fun onError(e: Throwable) {
                                getView()?.setSendBtnEnable(true)
                                getView()?.setSendBtnText("重试")
                            }

                            override fun onComplete() {
                                getView()?.setSendBtnEnable(true)
                                getView()?.setSendBtnText("重新发送")
                            }
                        })
            }

            override fun onError(error: String?) {
                getView()?.showToast(error)
            }

            override fun onStart() {
                getView()?.showLoadingDialog()
            }


            override fun onFinish() {
                getView()?.dismissLoadingDialog()
            }

        })
    }

    override fun createModel(): LoginContract.Model = LoginModel()

    override fun login() {

        val phone = getView()?.phone
        val code = getView()?.vCode
        val loginType = getView()?.loginType

        if (TextUtils.isEmpty(phone) || phone?.length != 11) {
            getView()?.showToast("请输入正确的手机号")
            return
        }

        if (loginType == LoginActivity.QUICK_LOGIN) {
            //快捷登录
            if (TextUtils.isEmpty(code) || code?.length != 6) {
                getView()?.showToast("请输入正确的验证码")
                return
            }
        } else {
            //密码登录
            if (TextUtils.isEmpty(code) || code?.length!! < 6) {
                getView()?.showToast("请输入正确的密码")
                return
            }
        }

        getModel().login(loginType!!, phone, code, object : XxBaseHttpObserver<LoginEntity>() {
            override fun onCompleted(msg: String?, entity: LoginEntity?) {
                getView()?.showToast("登录成功")
                Constants.putToken(entity!!.token)
                getView()?.loginSuccess()
            }

            override fun onError(error: String?) {
                getView()?.showToast(error)
            }

            override fun onStart() {
                getView()?.showLoadingDialog()
            }

            override fun onFinish() {
                getView()?.dismissLoadingDialog()
            }
        })
    }
}
